//
//  MSUINavigationController+Additions.m
//  MusouKit
//
//  Created by danal on 13-4-8.
//  Copyright (c) 2013年 danal. All rights reserved.
//

#import "MSAdditions.h"
#import "MSNSData+Additions.h"

@implementation UINavigationController (Musou)

#define kFlipDuration 0.7f
- (void)flipPopViewController{
    [UIView transitionWithView:self.view
                      duration:kFlipDuration
                       options:UIViewAnimationOptionTransitionFlipFromRight
                    animations:^{
                        [self popViewControllerAnimated:NO];
                    }
                    completion:^(BOOL finished){
                    }];
    
}

- (void)flipPushViewController:(UIViewController *)vc{
    [UIView transitionWithView:self.view
                      duration:kFlipDuration
                       options:UIViewAnimationOptionTransitionFlipFromLeft
                    animations:^{
                        [self pushViewController:vc animated:NO];
                    }
                    completion:^(BOOL finished){
                    }];

}

@end

@implementation NSBundle (Musou)

+ (NSString *)appVersion{
    return [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
}

+ (NSString *)pathForRes:(NSString *)res{
    return [[NSBundle mainBundle] pathForResource:res ofType:nil];
}

+ (NSURL *)URLForRes:(NSString *)res{
    return [[NSBundle mainBundle] URLForResource:res withExtension:nil];
}

+ (NSString *)pathForRes:(NSString *)res inBundle:(NSString *)bundle{
    return [[[NSBundle mainBundle] pathForResource:bundle ofType:nil] stringByAppendingPathComponent:res];
}

+ (NSURL *)URLForRes:(NSString *)res inBundle:(NSString *)bundle{
    NSString *path = [NSBundle pathForRes:res inBundle:bundle];
    return [NSURL URLWithString:path];
}

@end

@implementation NSFileManager (Musou)

+ (void)clearDirectory:(NSString *)dirPath complete:(void (^)(void))block{
    NSFileManager *fm = [NSFileManager defaultManager];
    NSArray *subpaths = [fm subpathsAtPath:dirPath];
    for (NSString *f in subpaths) {
        [fm removeItemAtPath:[dirPath stringByAppendingPathComponent:f] error:nil];
    }
    if (block != NULL) {
        block();
    }
}

@end

@implementation NSDate (Musou)

+ (NSString *)now{
    NSDateFormatter *df = [[NSDateFormatter alloc] init];
    df.dateFormat = @"yyyy-MM-dd HH:mm:ss";
    NSString *dateStr = [df stringFromDate:[NSDate date]];
    [df release];
    return dateStr;
}

+ (NSDate *)localDate{
    NSDate *date = [NSDate date];
    NSTimeZone *zone = [NSTimeZone systemTimeZone];
    NSInteger interval = [zone secondsFromGMTForDate: date];
    NSDate *localDate = [date  dateByAddingTimeInterval: interval];
    return localDate;
}

@end

@implementation NSDictionary (Musou)


- (NSNumber *)numberiForKey:(NSString *)key{
    id o = [self objectForKey:key];
    if ([o isKindOfClass:[NSNumber class]]) {
        return o;
    } else {
        return [NSNumber numberWithInt:[o intValue]];
    }
    
}

- (NSNumber *)numberfForKey:(NSString *)key{
    id o = [self objectForKey:key];
    if ([o isKindOfClass:[NSNumber class]]) {
        return o;
    } else {
        return [NSNumber numberWithFloat:[o floatValue]];
    }
    
}

- (NSString *)stringForKey:(NSString *)key{
    id o = [self objectForKey:key];
    if (o == [NSNull null]){
        return nil;
    }
    if ([o isKindOfClass:[NSNumber class]]) {
        return [NSString stringWithFormat:@"%@",o];
    } else {
        return o;
    }
}

- (NSArray *)arrayForKey:(NSString *)key{
    id o = [self objectForKey:key];
    if (o == [NSNull null]){
        return nil;
    }
    if ([o isKindOfClass:[NSArray class]]) {
        return o;
    }
    return nil;
}

- (NSDictionary *)dictForKey:(NSString *)key{
    id o = [self objectForKey:key];
    if (o == [NSNull null]){
        return nil;
    }
    if ([o isKindOfClass:[NSDictionary class]]) {
        return o;
    }
    return nil;
}

- (NSInteger)intForKey:(NSString *)key{
    return [[self objectForKey:key] intValue];
}

- (CGFloat)floatForKey:(NSString *)key{
    return [[self objectForKey:key] floatValue];
}

- (BOOL)boolForKey:(NSString *)key{
    return [[self objectForKey:key] boolValue];
}

@end


@implementation NSMutableDictionary (Musou)
- (void)setFloat:(CGFloat)f forKey:(NSString *)key{
    [self setObject:[NSNumber numberWithFloat:f] forKey:key];
}

- (void)setInt:(NSInteger)i forKey:(NSString *)key{
    [self setObject:[NSNumber numberWithInt:i] forKey:key];
}

@end

@interface NSMutableArray (Musou)

- (void)addFloat:(CGFloat)f;

- (void)addInt:(NSInteger)i;

@end


@implementation NSMutableArray (Musou)

- (void)addFloat:(CGFloat)f{
    [self addObject:[NSNumber numberWithFloat:f]];
}

- (void)addInt:(NSInteger)i{
    [self addObject:[NSNumber numberWithInt:i]];
}

@end


@implementation NSObject (Musou)

- (void)invokeMethod:(SEL)selector target:(id)target{
    NSMethodSignature *sign = [target methodSignatureForSelector:selector];
    NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:sign];
    [invocation setTarget:target];
    [invocation setSelector:selector];
    id ptr = self;
    [invocation setArgument:&ptr atIndex:2];
    [invocation invoke];
}

+ (NSString *)documentsDirectory{
    return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
}

+ (NSString *)cachesDirectory{
    return [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
}

+ (NSString *)pathInDocuments:(NSString *)file{
    return [[self documentsDirectory] stringByAppendingPathComponent:file];
}

+ (NSString *)pathInCaches:(NSString *)file{
    return [[self cachesDirectory] stringByAppendingPathComponent:file];
}

@end


@implementation NSString (Musou)

- (id)objectForKey:(NSString *)key{
    return nil;
}

- (NSString *)md5{
    return [self dataUsingEncoding:NSUTF8StringEncoding].md5;
}

- (NSString *)sha1{
    return [self dataUsingEncoding:NSUTF8StringEncoding].sha1;
}

- (NSString *)base64{
    return [self dataUsingEncoding:NSUTF8StringEncoding].base64;
}

@end


@implementation NSNull (Musou)

- (int)intValue{
    return 0;
}

- (float)floatValue{
    return 0.f;
}

- (int)longLongValue{
    return 0;
}

- (BOOL)boolValue{
    return NO;
}

@end


@implementation NSNumber (Musou)

- (int)length{
    NSString *str = [NSString stringWithFormat:@"%@",self];
    return [str length];
}

+ (NSNumber *)numberi:(int)i{
    return [NSNumber numberWithInt:i];
}

+ (NSNumber *)numberf:(int)f{
    return [NSNumber numberWithFloat:f];
}

@end


@implementation UIView (Musou)

- (void)setOrigin:(CGPoint)origin{
    self.frame = CGRectMake(origin.x, origin.y, self.bounds.size.width, self.bounds.size.height);
}

- (void)setSize:(CGSize)size{
    self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, size.width, size.height);
}

+ (UIView *)loadFromNib{
#ifdef DEBUG
    NSLog(@"LoadFromNib: %@",NSStringFromClass([self class]));
#endif
    return [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass([self class]) owner:nil options:nil] lastObject];
}

@end